﻿using QuanLyTiemVang.BUS;
using QuanLyTiemVang.Classes;
using QuanLyTiemVang.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace QuanLyTiemVang.GUI
{
    /// <summary>
    /// Interaction logic for BaoCaoTonKho.xaml
    /// </summary>
    public partial class BaoCaoTonKho : Window, IPhanQuyenHelper
    {
        private int maBC;
        public BaoCaoTonKho()
        {
            InitializeComponent();
            this.Loaded += BaoCaoTonKho_Loaded;
            List<BAOCAOTON> lstBaoCao = BaoCaoTonDAO.SelectAllBaoCaoTon();
            if (lstBaoCao.Count == 0)
            {
                this.txtMaBaoCao.Text = "0";
            }
            else
            {
                this.txtMaBaoCao.Text = (lstBaoCao[lstBaoCao.Count - 1].MaBaoCaoTon + 1).ToString();
            }
            dpNgayBaoCao.Text = DateTime.Now.ToShortDateString();

            //Kiểm tra báo cáo tồn
            
            int maBaoCaoTon = 0;
            if (lstBaoCao != null)
                foreach (var bct in lstBaoCao)
                {
                    if (bct.NgayBaoCao.Value.Date.Month == DateTime.Now.Date.Month && bct.NgayBaoCao.Value.Date.Year == DateTime.Now.Date.Year)
                    {
                        maBaoCaoTon = bct.MaBaoCaoTon;
                    }
                }

            if (maBaoCaoTon == 0)
            {
                BAOCAOTON bct = new BAOCAOTON
                {
                    NgayBaoCao = DateTime.Now.Date
                };
                BaoCaoTonBUS.ThemBaoCaoTon(bct);
                maBaoCaoTon = bct.MaBaoCaoTon;
                List<SANPHAM> lstSP = SanPhamDAO.GetAllSanPham();
                if (lstSP != null)
                {
                    foreach (var item in lstSP)
                    {
                        CHITIETBAOCAOTON CTBCT = new CHITIETBAOCAOTON
                        {
                            MaBaoCaoTon = maBaoCaoTon,
                            MaSanPham = item.MaSanPham,
                            TonDauKy = item.SoLuongTon,
                            SoLuongBan = 0,
                            SoLuongMua = 0,
                            TonCuoiKy = item.SoLuongTon
                        };

                        ChiTietBaoCaoTonBUS.ThemChiTietBaoCaoTon(CTBCT);
                    }
                }
            }
        }

        void BaoCaoTonKho_Loaded(object sender, RoutedEventArgs e)
        {
            CurrentUser.Instance.AddForm(this);
        }
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);

            // Begin dragging the window
            this.DragMove();
        }

        public void SetAdminFunction()
        {
        }

        public void SetStaffFunction()
        {
        }

        public void SetCustomerFunction()
        {
        }

        public void SetNonFunction()
        {
        }

        private void btnThoat_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnLapBaoCao_Click(object sender, RoutedEventArgs e)
        {
            List<CHITIETBAOCAOTON> lstCTBCT = ChiTietBaoCaoTonDAO.SelectAllChiTietBaoCaoTon();
            List<BAOCAOTON> lsbct = BaoCaoTonDAO.SelectAllBaoCaoTon();
            List<CHITIETBAOCAOTON> result = new List<CHITIETBAOCAOTON>();
            foreach (var item in lstCTBCT)
            {
                foreach (var item2 in lsbct)
                {
                    if (item.MaBaoCaoTon == item2.MaBaoCaoTon && dpNgayBaoCao.SelectedDate.Value.Date.Month == item2.NgayBaoCao.Value.Date.Month && dpNgayBaoCao.SelectedDate.Value.Date.Year == item2.NgayBaoCao.Value.Date.Year)
                    {
                        maBC = item2.MaBaoCaoTon;
                        result.Add(item);
                    }
                }
            }
            dgThongTinPhieuBaoCaoTonKho.ItemsSource = null;
            dgThongTinPhieuBaoCaoTonKho.ItemsSource = result;
            this.btnInBaoCao.Visibility = Visibility.Visible;
        }

        private void btnInBaoCao_Click_1(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
            sfd.ShowDialog();

            ReportBUS.TonKhoReport(maBC, sfd.FileName + ".pdf");
            MessageBox.Show("Đã in phiếu thành công.", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
        }
    }
}
